package repository

import (
	"fitness/go-admin/internal/model"

	"gorm.io/gorm"
)

type DictRepository struct {
	db *gorm.DB
}

func NewDictRepository(db *gorm.DB) *DictRepository {
	return &DictRepository{db: db}
}

// DictType 相关方法

// CreateDictType 创建字典类型
func (r *DictRepository) CreateDictType(dictType *model.DictType) error {
	return r.db.Create(dictType).Error
}

// GetDictTypeByID 根据ID获取字典类型
func (r *DictRepository) GetDictTypeByID(id uint) (*model.DictType, error) {
	var dictType model.DictType
	err := r.db.Preload("DictData").Where("id = ?", id).First(&dictType).Error
	return &dictType, err
}

// GetDictTypeByType 根据类型获取字典类型
func (r *DictRepository) GetDictTypeByType(dictType string) (*model.DictType, error) {
	var dt model.DictType
	err := r.db.Where("type = ?", dictType).First(&dt).Error
	return &dt, err
}

// ListDictTypes 获取字典类型列表
func (r *DictRepository) ListDictTypes(name, dictType string, status int, offset, limit int) ([]*model.DictType, int64, error) {
	var dictTypes []*model.DictType
	var total int64

	db := r.db.Model(&model.DictType{})

	// 添加查询条件
	if name != "" {
		db = db.Where("name LIKE ?", "%"+name+"%")
	}
	if dictType != "" {
		db = db.Where("type LIKE ?", "%"+dictType+"%")
	}
	if status != 0 {
		db = db.Where("status = ?", status)
	}

	// 获取总数
	if err := db.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 获取数据
	err := db.Offset(offset).Limit(limit).Order("created_at DESC").Find(&dictTypes).Error
	return dictTypes, total, err
}

// UpdateDictType 更新字典类型
func (r *DictRepository) UpdateDictType(dictType *model.DictType) error {
	return r.db.Save(dictType).Error
}

// DeleteDictType 删除字典类型
func (r *DictRepository) DeleteDictType(id uint) error {
	return r.db.Delete(&model.DictType{}, id).Error
}

// DictData 相关方法

// CreateDictData 创建字典数据
func (r *DictRepository) CreateDictData(dictData *model.DictData) error {
	return r.db.Create(dictData).Error
}

// GetDictDataByID 根据ID获取字典数据
func (r *DictRepository) GetDictDataByID(id uint) (*model.DictData, error) {
	var dictData model.DictData
	err := r.db.Where("id = ?", id).First(&dictData).Error
	return &dictData, err
}

// ListDictData 获取字典数据列表
func (r *DictRepository) ListDictData(dictTypeID uint, label, value string, status int, offset, limit int) ([]*model.DictData, int64, error) {
	var dictData []*model.DictData
	var total int64

	db := r.db.Model(&model.DictData{})

	// 添加查询条件
	if dictTypeID != 0 {
		db = db.Where("dict_type_id = ?", dictTypeID)
	}
	if label != "" {
		db = db.Where("label LIKE ?", "%"+label+"%")
	}
	if value != "" {
		db = db.Where("value LIKE ?", "%"+value+"%")
	}
	if status != 0 {
		db = db.Where("status = ?", status)
	}

	// 获取总数
	if err := db.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 获取数据
	err := db.Offset(offset).Limit(limit).Order("sort ASC, created_at DESC").Find(&dictData).Error
	return dictData, total, err
}

// GetDataByDictType 根据字典类型获取字典数据
func (r *DictRepository) GetDataByDictType(dictType string) ([]*model.DictData, error) {
	var dictData []*model.DictData

	// 先获取字典类型
	dictTypeModel, err := r.GetDictTypeByType(dictType)
	if err != nil {
		return nil, err
	}

	// 再获取字典数据
	err = r.db.Where("dict_type_id = ? AND status = 1", dictTypeModel.ID).Order("sort ASC").Find(&dictData).Error
	return dictData, err
}

// UpdateDictData 更新字典数据
func (r *DictRepository) UpdateDictData(dictData *model.DictData) error {
	return r.db.Save(dictData).Error
}

// DeleteDictData 删除字典数据
func (r *DictRepository) DeleteDictData(id uint) error {
	return r.db.Delete(&model.DictData{}, id).Error
}

// BatchDeleteDictData 批量删除字典数据
func (r *DictRepository) BatchDeleteDictData(ids []uint) error {
	return r.db.Where("id IN ?", ids).Delete(&model.DictData{}).Error
}
